package cn.openea.perpartner.payment.web.subpoints.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.StrUtil;
import cn.openea.perpartner.common.core.utils.Arith;
import cn.openea.perpartner.payment.web.points.dto.PointsDto;
import cn.openea.perpartner.payment.web.points.service.IPointsService;
import cn.openea.perpartner.payment.web.points.service.IRateCenterService;
import cn.openea.perpartner.payment.web.subpoints.dto.SubPointDto;
import cn.openea.perpartner.payment.web.subpoints.entity.SubPointLog;
import cn.openea.perpartner.payment.web.subpoints.entity.SubPoint;
import cn.openea.perpartner.payment.web.subpoints.service.ISubPointLogService;
import cn.openea.perpartner.payment.web.subpoints.service.ISubPointService;
import cn.openea.perpartner.payment.web.subpoints.service.ISubPaymentService;
import cn.openea.perpartner.payment.web.wallet.dto.WalletDto;
import cn.openea.perpartner.payment.web.wallet.service.IWalletService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author Weird077
 * @description TODO
 * @date 2022-06-27 15:40
 */
@Service("subPaymentService")
public class SubPaymentServiceImpl implements ISubPaymentService {
    @Resource
    private ISubPointLogService subPaymentLogService;
    @Resource
    private ISubPointService subPaymentPointsService;
    @Resource
    private IPointsService pointsService;
    @Resource
    private IRateCenterService rateCenterService;
    @Resource
    private IWalletService walletService;

    @Override
    public List<SubPointLog> selectList(String userId, String sceneId, String operType) {
        return subPaymentLogService.selectList(userId, sceneId,operType);
    }

    @Override
    public SubPoint selectPointsByUserId(String userId, String sceneId) {
        return subPaymentPointsService.selectPointsByUserId(userId,sceneId);
    }

    @Transactional(rollbackFor = {RuntimeException.class, Error.class})
    @Override
    public boolean add(SubPointDto paymentDto) {
        subPaymentPointsService.updateByUserId(paymentDto, "add");
        SubPointLog paymentLog = new SubPointLog();
        BeanUtil.copyProperties(paymentDto, paymentLog, true);
        paymentLog.setSceneId(paymentDto.getSceneId());
        return subPaymentLogService.save(paymentLog, "add");
    }

    @Transactional(rollbackFor = {RuntimeException.class, Error.class})
    @Override
    public boolean sub(SubPointDto paymentDto) {
        boolean isSuccess = subPaymentPointsService.updateByUserId(paymentDto, "sub");
        if (isSuccess) {
            SubPointLog paymentLog = new SubPointLog();
            BeanUtil.copyProperties(paymentDto, paymentLog, true);
            paymentLog.setSceneId(paymentDto.getSceneId());
            isSuccess = subPaymentLogService.save(paymentLog, "sub");
        }
        return isSuccess;
    }

    @Transactional(rollbackFor = {RuntimeException.class, Error.class})
    @Override
    public boolean extract(SubPointDto dto) {
        //1、获取积分兑换比例
        double rate = rateCenterService.getRate(dto.getSceneId());
        //计算需要扣除的平台积分
        double points = Arith.div(Convert.toDouble(dto.getPoints()),rate);
        PointsDto pointsDto = new PointsDto();
        BeanUtil.copyProperties(dto, pointsDto,true);
        pointsDto.setPoints(Convert.toStr(points));
        //1、扣减平台积分 2、增加子账户积分
        return pointsService.sub(pointsDto) && add(dto);
    }

    @Override
    public double getTotalPoints(String userId,String sceneId) {
        List<SubPointLog> list = selectList(userId, sceneId,"add");
        return list.stream().mapToDouble(obj -> Double.parseDouble(obj.getPoints())).sum();
    }
    @Transactional(rollbackFor = {RuntimeException.class, Error.class})
    @Override
    public boolean pointsToWallet(SubPointDto dto) {
        if (StrUtil.isBlank(dto.getReason())){
            dto.setReason("转入钱包");
        }
        //1、先增加场景积分
        boolean isSuccess = add(dto);
        //2、扣减场景积分
        if (isSuccess){
            isSuccess = sub(dto);
        }
        if(isSuccess){
            //3、获取积分兑换比例
            double rate = rateCenterService.getRate(dto.getSceneId());
            //4、计算增加的平台积分
            double points = Arith.mul(Convert.toDouble(dto.getPoints()),rate);
            PointsDto pointsDto = new PointsDto();
            BeanUtil.copyProperties(dto, pointsDto,true);
            pointsDto.setPoints(Convert.toStr(points));
            //5、增加平台积分
            isSuccess = pointsService.add(pointsDto);
            //6、转入钱包
            if(isSuccess){
                WalletDto wallet = new WalletDto();
                wallet.setPoints(Convert.toStr(points));
                wallet.setUserId(dto.getUserId());
                wallet.setReason(dto.getReason());
                wallet.setSceneId(dto.getSceneId());
                walletService.pointsToWallet(wallet);
            }
        }
        return isSuccess;
    }

    @Transactional(rollbackFor = {RuntimeException.class, Error.class})
    @Override
    public boolean redeem(SubPointDto dto) {
        //1、扣减子账户积分
        boolean isSuccess = sub(dto);
        //2、增加平台积分
        if(isSuccess){
            //3、获取积分兑换比例
            double rate = rateCenterService.getRate(dto.getSceneId());
            //4、计算增加的平台积分
            double points = Arith.mul(Convert.toDouble(dto.getPoints()),rate);
            PointsDto pointsDto = new PointsDto();
            BeanUtil.copyProperties(dto, pointsDto,true);
            pointsDto.setPoints(Convert.toStr(points));
            isSuccess = pointsService.add(pointsDto);
        }
        return isSuccess;
    }
}
